package backs.dataBase;

import java.util.List;

public class DBNormal
{
    public static String getValue(String db,int id,String key)
    {
        return getValue( db, id, key, null,false);
    }
    public static String getValue(String db,int id,String key,String defaultValue,boolean reset)
    {
        String value=QAIDB.getValue(db,id,key);
        if(value==null)
        {
            value=defaultValue;
            if(reset&&value!=null)
            {
                QAIDB.insertValue(db,id,key,value);
            }
        }
        return value;
    }

    public static List<String> getKeysLike(String db, int id, String key,String value)
    {
        return QAIDB.getKeysLike(db,id,key,value);
    }

    public static List<String> getValuesLike(String db, int id, String key,String value)
    {
        return QAIDB.getValuesLike(db,id,key,value);
    }

    public static List<String> getValues(String db, int id, String key)
    {
        return QAIDB.getValues(db,id,key);
    }

    public static boolean insertValue(String db,int id,String key,String value)
    {
        if(getValue(db,id,key,null,false)==null)
        {
            return QAIDB.insertValue(db,id,key,value);
        }
        return false;
    }

    public static boolean insertListValue(String db,int id,String key,String value)
    {
        return QAIDB.insertValue(db,id,key,value);
    }

    public static boolean insertIfNotListValue(String db,int id,String key,String value)
    {
        if(getValue(db,id,key).contains(value)) return false;
        return QAIDB.insertValue(db,id,key,value);
    }

    public static boolean setValue(String db,int id,String key,String value)
    {
        return setValue(db,id,key,value,false);
    }

    public static boolean setValue(String db,int id,String key,String value,boolean insertWhenNotExist)
    {
        if(getValue(db,id,key,null,false)==null)
        {
            if(insertWhenNotExist)
            {
                return insertValue(db,id,key,value);
            }
            else
            {
                return false;
            }
        }
        else return QAIDB.updateValue(db,id,key,value);
    }

    public static boolean delValue(String db,int id,String key,String value)
    {
        if(value!=null)
        {
            return QAIDB.delValue(db,id,key,value);
        }
        else
        {
            if(key!=null)
            {
                return QAIDB.delValue(db,id,key);
            }
            else return QAIDB.delValue(db,id);
        }
    }

    public static boolean delValue(String db,int id,String key)
    {
        return delValue(db,id,key,null);
    }

    public static boolean delValue(String db,int id)
    {
        return delValue(db,id,null);
    }

    public static int getIDAndIncrease(String db,int id,String key,int defaultValue)
    {
        String nowIDS= DBNormal.getValue(db,id,key,defaultValue+"",true);
        int nowID=Integer.parseInt(nowIDS);
        DBNormal.setValue(db,id,key,(nowID+1)+"",true);
        return nowID;
    }
}
